package com.iqiyi.pps.epg.core.model.auth;

import com.iqiyi.kiwi.utils.ConvertUtils;
import com.iqiyi.pps.epg.core.model.base.BaseEntity;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "show_auth_resource")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class Resource extends BaseEntity {
    private static final long serialVersionUID = 5818151052573478132L;
	// resourceType常量
	public static final String TYPE_URL = "url";
	public static final String TYPE_MENU = "menu";
    private String type; // 资源类型
    private String name; // 资源名称
	private String value; // 资源标识
    private String description; // 资源标识
	private Double priority = 0.0; // 资源显示顺序字段
    private Boolean enabled = false; // 资源是否可用
    protected Long id;



	public void setId(Long id) {
		this.id = id;
	}

	private Set<Authority> authorities = new LinkedHashSet<Authority>(); // 可访问该资源的授权
    
    
    @Id
    @Override
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
		return id;
	}
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
    @Column(name = "priority")
	public double getPriority() {
		return priority;
	}

	public void setPriority(double priority) {
		this.priority = priority;
	}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @ManyToMany(fetch = FetchType.EAGER)
	@Cascade( { org.hibernate.annotations.CascadeType.MERGE })
	@JoinTable(name = "show_auth_authority_resource", joinColumns = { @JoinColumn(name = "RESOURCE_ID") }, inverseJoinColumns = { @JoinColumn(name = "AUTHORITY_ID") })
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<Authority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set<Authority> authorities) {
		this.authorities = authorities;
	}

	/* 得到被控方--Authority的相关信息 */
	@Transient
	public String getAuthNames() {
		String authName = ConvertUtils.convertElementPropertyToString(authorities, "prefixedName", ",");
		return authName;
	}

	@Transient
	public List<Long> getAuthIds() {
		return ConvertUtils.convertElementPropertyToList(authorities, "id");
	}




}
